<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( '../global/initialize.php' );
aos_header( 'Top villages' );
global $dal;

if( $_GET[ 'type' ] == 'population' ) {
?>
				<h2><?php transprint( 'Top 5 villages based on population' ); ?></h2>
<?php
	$statement = $dal->prepare( 'SELECT village_id, name, x, y ' .
			'FROM village ' .
			'WHERE ruler_id != -1 ' .
			'ORDER BY population DESC ' .
			'LIMIT 5' );
}
elseif( $_GET[ 'type' ] == 'food' ) {
?>
				<h2><?php transprint( 'Top 5 villages based on food production' ); ?></h2>
<?php
	$statement = $dal->prepare( 'SELECT village_id, name, x, y ' .
			'FROM village ' .
			'WHERE ruler_id != -1 AND ( SELECT SUM( ammount * food_income ) ' .
				'FROM village_buildings ' .
				'JOIN buildings ON village_buildings.building_id = buildings.building_id ' .
				'WHERE village_buildings.village_id = village.village_id ) > 0 ' .
			'ORDER BY ( SELECT SUM( ammount * food_income ) ' .
				'FROM village_buildings ' .
				'JOIN buildings ON village_buildings.building_id = buildings.building_id ' .
				'WHERE village_buildings.village_id = village.village_id ) * food_quality DESC ' .
			'LIMIT 5' );
}
elseif( $_GET[ 'type' ] == 'coal' ) {
?>
				<h2><?php transprint( 'Top 5 villages based on coal production' ); ?></h2>
<?php
	$statement = $dal->prepare( 'SELECT village_id, name, x, y ' .
			'FROM village ' .
			'WHERE ruler_id != -1 AND ( SELECT SUM( ammount * coal_income ) ' .
				'FROM village_buildings ' .
				'JOIN buildings ON village_buildings.building_id = buildings.building_id ' .
				'WHERE village_buildings.village_id = village.village_id ) > 0 ' .
			'ORDER BY ( SELECT SUM( ammount * coal_income ) ' .
				'FROM village_buildings ' .
				'JOIN buildings ON village_buildings.building_id = buildings.building_id ' .
				'WHERE village_buildings.village_id = village.village_id ) * coal_quality DESC ' .
			'LIMIT 5' );
}
elseif( $_GET[ 'type' ] == 'iron' ) {
?>
				<h2><?php transprint( 'Top 5 villages based on iron production' ); ?></h2>
<?php
	$statement = $dal->prepare( 'SELECT village_id, name, x, y ' .
			'FROM village ' .
			'WHERE ruler_id != -1 AND ( SELECT SUM( ammount * iron_income ) ' .
				'FROM village_buildings ' .
				'JOIN buildings ON village_buildings.building_id = buildings.building_id ' .
				'WHERE village_buildings.village_id = village.village_id ) > 0 ' .
			'ORDER BY ( SELECT SUM( ammount * iron_income ) ' .
				'FROM village_buildings ' .
				'JOIN buildings ON village_buildings.building_id = buildings.building_id ' .
				'WHERE village_buildings.village_id = village.village_id ) * iron_quality DESC ' .
			'LIMIT 5' );
}
?>
				<br />
				<br />
<?php

$villages = $dal->getArrayOfObjects( $statement );

if( count( $villages ) > 0 ) {
	$top = 1;
	$statement = $dal->prepare( 'SELECT ruler_id ' .
			'FROM village ' .
			'WHERE village_id = :village_id' );
	$statement2 = $dal->prepare( 'SELECT faction_id ' .
			'FROM faction_member ' .
			'WHERE ruler_id = :ruler_id AND invited = 0 AND requested = 0' );
	$statement2->bindParam( ':ruler_id', $_SESSION[ 'ruler_id' ], PDO::PARAM_INT );
	$factionId = $dal->getValue( $statement2 );
	foreach( $villages AS $village ) {
		$statement->bindParam( ':village_id', $village->village_id, PDO::PARAM_INT );
		$villageRulerId = $dal->getValue( $statement );
		if( $villageRulerId == $_SESSION[ 'ruler_id' ] ) {
			$strong = '<a href="../villages/village.php?name=' . $village->name . '"><strong class="green">';
			$strongEnd = '</strong></a>';
		}
		else {
			$statement2->bindParam( ':ruler_id', $villageRulerId, PDO::PARAM_INT );
			$factionId2 = $dal->getValue( $statement2 );
			if( isset( $factionId ) && isset( $factionId2 ) && $factionId == $factionId2 ) {
				$strong = '<a href="../operations/map.php?x=' . ( $village->x - 20 ) . '&y=' . ( $village->y - 20 ) . '"<strong class="blue">';
				$strongEnd = '</strong></a>';
			}
			else {
				$strong = '<strong class="red">';
				$strongEnd = '</strong>';
			}
		}
		printVillageMap( $village->village_id, $top . '. ' . $strong . $village->name . $strongEnd );
		$top ++;
	}
}
else {
?>
						<div class="blok">
							<?php transprint( 'No villages found.' ); ?>
						</div>
<?php	
}

aos_footer();
?>